package com.qkl.upload.publics.utils;

import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.annotation.JsonIgnore;
import lombok.Data;

@Data
public class Result {
	/**
	 * 响应代码
	 */
	private Integer code;

	/**
	 * 响应消息
	 */
	private String message;

	/**
	 * 响应结果
	 */
	private Object data;

	public Result() {
	}

	public Result(BaseErrorInfo errorInfo) {
		this.code = errorInfo.getResultCode();
		this.message = errorInfo.getResultMsg();
	}

	public Integer getCode() {
		return code;
	}

	public void setCode(Integer code) {
		this.code = code;
	}

	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}

	public Object getData() {
		return data;
	}

	public void setData(Object data) {
		this.data = data;
	}

	/**
	 * 成功
	 * 
	 * @return
	 */
	public static Result success() {
		return success(null);
	}

	/**
	 * 成功
	 * @param data
	 * @return
	 */
	public static Result success(Object data) {
		Result rb = new Result();
		rb.setCode(CommonEnum.SUCCESS.getResultCode());
		rb.setMessage(CommonEnum.SUCCESS.getResultMsg());
		rb.setData(data);
		return rb;
	}

	/**
	 * 成功
	 * @param data
	 * @param msg
	 * @return
	 */
	public static Result success(Object data,String msg) {
		Result rb = new Result();
		rb.setCode(CommonEnum.SUCCESS.getResultCode());
		rb.setMessage(msg);
		rb.setData(data);
		return rb;
	}

	/**
	 * 失败
	 */
	public static Result error(BaseErrorInfo errorInfo) {
		Result rb = new Result();
		rb.setCode(errorInfo.getResultCode());
		rb.setMessage(errorInfo.getResultMsg());
		rb.setData(null);
		return rb;
	}

	/**
	 * 失败
	 */
	public static Result error(Integer code, String message) {
		Result rb = new Result();
		rb.setCode(code);
		rb.setMessage(message);
		rb.setData(null);
		return rb;
	}

	/**
	 * 失败
	 */
	public static Result error(String message) {
		Result rb = new Result();
		rb.setCode(-1);
		rb.setMessage(message);
		rb.setData(null);
		return rb;
	}

	public boolean resultSuccess(){
		return CommonEnum.SUCCESS.getResultCode().equals(code);
	}

	@Override
	public String toString() {
		return JSONObject.toJSONString(this);
	}

	@JsonIgnore
	private String onlTable;

}

